package com.yxjx.duoxue.utils;

import java.util.HashMap;

import android.app.Activity;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWebPage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.yxjx.duoxue.Constants;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.ShareTextHelper;

public class ShareUtils {

	/**
	 * Share Function: 对所有的分享类型设置一套参数。
	 * @param activity
	 * @param shareUrl
	 * @param shareObject
	 * @return
	 */
	public static UMSocialService initShareController(Activity activity,
			String shareUrl,
			Object shareObject) {
		UMSocialService controller = UMServiceFactory.getUMSocialService("com.umeng.share");
		// 设置分享内容
		// 设置分享图片, 参数2为图片的url地址
		String shareContent = ShareTextHelper.getShareText(shareObject)
				+ shareUrl;
		controller.setShareContent(shareContent);
		String imgUrl = ShareTextHelper.getShareImageUrl(shareObject);
		UMImage image;
		if (UIHelper.isEmpty(imgUrl)) {
			image = new UMImage(activity, R.drawable.logo);
		} else {
			image = new UMImage(activity, imgUrl);
		}
		image.setTargetUrl(shareUrl);
		CircleShareContent circleShareContent = new CircleShareContent();
		circleShareContent.setShareImage(image);
		circleShareContent.setShareContent(shareContent);
		circleShareContent.setTitle(shareContent);
		controller.setShareMedia(circleShareContent);
		SHARE_MEDIA[] channels = new SHARE_MEDIA[] {
				SHARE_MEDIA.WEIXIN,
				SHARE_MEDIA.WEIXIN_CIRCLE,
				SHARE_MEDIA.QQ,
				SHARE_MEDIA.QZONE,
				SHARE_MEDIA.SINA
		};
		for (SHARE_MEDIA channel : channels) {
			controller.setAppWebSite(channel, shareUrl);
		}
		
		// customize
		controller.getConfig().removePlatform(SHARE_MEDIA.TENCENT);
		initQQ(activity);
		initQzone(activity);
		initWX(activity);
		return controller;
	}
	
	/**
	 * 根据分享的类型来customzie分享的参数
	 * @param activity
	 * @param shareUrl
	 * @param shareObject
	 * @param drawableId
	 * @return
	 */
	public static UMSocialService initShareController(Activity activity,
			String shareUrl,
			Object shareObject,
			int drawableId) {
		UMSocialService controller = UMServiceFactory.getUMSocialService("com.umeng.share");
		// 设置分享内容
		// 设置分享图片, 参数2为图片的url地址
		String shareContent = ShareTextHelper.getShareText(shareObject);

		// 新浪微博分享文本和链接；其他渠道只分纯文本
		if (drawableId == R.drawable.t_weibo) {
			shareContent = shareContent + shareUrl;
		}
		controller.setShareContent(shareContent);
		// Image对象
		String imgUrl = ShareTextHelper.getShareImageUrl(shareObject);
		UMImage image;
		if (UIHelper.isEmpty(imgUrl)) {
			image = new UMImage(activity, R.drawable.logo);
		} else {
			image = new UMImage(activity, imgUrl);
		}
		// 微信朋友圈要分享特殊的对象
		image.setTargetUrl(shareUrl);
		if (drawableId == R.drawable.t_pyq) {
			CircleShareContent circleShareContent = new CircleShareContent();
			circleShareContent.setShareImage(image);
			circleShareContent.setShareContent(shareContent);
			circleShareContent.setTitle(shareContent);
			controller.setShareMedia(circleShareContent);
		} else {
			controller.setShareMedia(image);
		}
		
		SHARE_MEDIA[] channels = new SHARE_MEDIA[] {
				SHARE_MEDIA.WEIXIN,
				SHARE_MEDIA.WEIXIN_CIRCLE,
				SHARE_MEDIA.QQ,
				SHARE_MEDIA.QZONE,
				SHARE_MEDIA.SINA
		};
		for (SHARE_MEDIA channel : channels) {
			controller.setAppWebSite(channel, shareUrl);
		}
		// customize
		controller.getConfig().removePlatform(SHARE_MEDIA.TENCENT);
		initQQ(activity);
		initQzone(activity);
		initWX(activity);
		return controller;
	}
	
	private static void initQQ(Activity activity) {
		//参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, Constants.QQ_APP_ID,
		                Constants.QQ_APP_KEY);
		qqSsoHandler.addToSocialSDK(); 
	}
	
	private static void initQzone(Activity activity) {
		//参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, Constants.QQ_APP_ID,
		                Constants.QQ_APP_KEY);
		qZoneSsoHandler.addToSocialSDK();
	}
	
	private static void initWX(Activity activity) {
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appID = Constants.WX_APP_ID;
		String appSecret = Constants.WX_APP_SECRET;
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(activity,appID,appSecret);
		wxHandler.addToSocialSDK();
		// 添加微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(activity,appID,appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
	}
	// End;
	
	/**
	 * drawableId to SHARE_MEDIA
	 * @param drawableId
	 * @return
	 */
	public static SHARE_MEDIA drawableToChannel(int drawableId) {
		HashMap<Integer, SHARE_MEDIA> map = new HashMap<Integer, SHARE_MEDIA>() {
			{
				put(R.drawable.t_wxhy, SHARE_MEDIA.WEIXIN);
				put(R.drawable.t_pyq, SHARE_MEDIA.WEIXIN_CIRCLE);
				put(R.drawable.t_qq, SHARE_MEDIA.QQ);
				put(R.drawable.t_qzone, SHARE_MEDIA.QZONE);
				put(R.drawable.t_weibo, SHARE_MEDIA.SINA);
				
			}
		};
		if (map.containsKey(drawableId)) {
			return map.get(drawableId);
		} else {
			return null;
		}
	}
}
